package com.shengde.jinwawaforleader.model.myJob.classStatus;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.adapter.ClassStatusAdapter;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassStatus;
import com.umeng.socialize.UMShareAPI;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

import static com.shengde.jinwawaforleader.HomeApplication.showToastShort;

/**
 * 班级动态
 * Created by Administrator on 2016/9/1 0001.
 */
public class ClassStatusActivity extends BaseActivity  {
	private static final String TAG = "ClassStatusActivity";
	private static final int FIRST_IN = 1;
	private static final int REFRESH = 2;
	private static final int ADD_SUCCEED = 3;
	private TextView tv_back;
	private TextView tv_title;
	private LinearLayout loading_main;  //加载中..


	List<ProClassStatus.DynamicBean> DynamicBeans;
	private List<ProClassStatus.DynamicBean> DynamicBean;
	private ClassStatusAdapter classStatusAdapter;
	private PullToRefreshListView pull_refresh_list; //下拉刷新 和上拉加载跟多
	private ListView listview_class_status;   //下拉刷新的listview
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FIRST_IN:

					classStatusAdapter = new ClassStatusAdapter(ClassStatusActivity.this, DynamicBeans);
					listview_class_status.setAdapter(classStatusAdapter);
					loading_main.setVisibility(View.GONE);
					classStatusAdapter.notifyDataSetChanged();
					break;
				case REFRESH:
					judge();
					showToastShort(getString(R.string.refresh_success));
					break;
				case ADD_SUCCEED:
					judge();
					showToastShort(getString(R.string.loading_success));
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.class_status_activity);
	}

	@Override
	public void initView() {

		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		loading_main = (LinearLayout) findViewById(R.id.loading_main);
		pull_refresh_list = (PullToRefreshListView) findViewById(R.id.ptr_listview);
		listview_class_status = pull_refresh_list.getRefreshableView();

	}

	@Override
	public void initData() {
		DynamicBeans = new ArrayList();
		tv_title.setText(getString(R.string.class_status));
		pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.xlistview_footer_hint_normal));
		pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.xlistview_footer_hint_ready));
		showClassStatus(System.currentTimeMillis() / 1000, FIRST_IN);

	}


	@Override
	public void initListener() {

		tv_back.setOnClickListener(this);

		//刷新监听
		pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				DynamicBeans.clear();
				showClassStatus(System.currentTimeMillis() / 1000, REFRESH);
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				if (DynamicBean.size() == 0) {
					pull_refresh_list.onRefreshComplete();
					showToastShort(getString(R.string.xlistview_footer_no_more_msg));
				}else {
					showClassStatus(DynamicBean.get(DynamicBean.size() - 1).getLasttime() / 1000, ADD_SUCCEED);
				}
			}
		});

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}


	//判断 刷新状态后的 调用方法
	private void judge() {
		pull_refresh_list.onRefreshComplete();
		if (classStatusAdapter == null) {
			classStatusAdapter = new ClassStatusAdapter(ClassStatusActivity.this, DynamicBeans);
			listview_class_status.setAdapter(classStatusAdapter);
		} else {
			classStatusAdapter.notifyDataSetChanged();
		}
	}

	/**
	 * 显示的班级动态
	 */
	private void showClassStatus(long time, final int flag) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/classDynamic";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/classDynamic";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("schoolid", User.getSchoolId(this));
		params.addParameter("userid", User.getUserLoginAccount(this));
		params.addParameter("uninx", time);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProClassStatus pro_class_status = new Gson().fromJson(result, ProClassStatus.class);
				if (pro_class_status != null && pro_class_status.getCode() == 0) {
					DynamicBean = pro_class_status.getDynamic();
					for (int i = 0; i < DynamicBean.size(); i++) {
						DynamicBeans.add(DynamicBean.get(i));
					}
					mHandler.sendEmptyMessage(flag);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
		showClassStatus(System.currentTimeMillis() / 1000, FIRST_IN);
	}
	@Override
	protected void onPause() {
		super.onPause();
		JCVideoPlayer.releaseAllVideos();
	}

}
